package com.huiminpay.merchant.controller;

import com.aliyun.oss.OSS;
import com.huiminpay.merchant.api.MerchantService;
import com.huiminpay.merchant.covert.MerchantDetailVOconvert;
import com.huiminpay.merchant.dto.MerchantDTO;
import com.huiminpay.merchant.covert.MerchantRegisterConvert;
import com.huiminpay.merchant.service.SmsService;
import com.huiminpay.merchant.util.SecurityUtil;
import com.huiminpay.merchant.vo.MerchantDetailVO;
import com.huiminpay.merchant.vo.MerchantRegisterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Log4j2
@Api(value = "MerchantController",tags = "商户服务接口")
@RestController
public class MerchantController {
    @Reference//dubbo的服务
    private MerchantService merchantService;
    @Autowired
    private SmsService smsService;
    @ApiOperation(value = "findById",notes = "传入用户ID，返回对应的用户信息，ID必须为正整数",response = MerchantDTO.class,httpMethod = "GET")
    @ApiImplicitParam(name = "id",value = "商户ID",dataType = "Long",paramType = "path",required = true)
    @GetMapping("/merchants/{id}")
    public MerchantDTO  findById(@PathVariable  Long id){
        MerchantDTO merchantDTO = merchantService.queryMerchantById(id);
        return merchantDTO;
    }
    //发送验证码
    @GetMapping("/sms")
    public String getSmsCode(@RequestParam String phone){
        log.info("手机号:{}",phone);
        return smsService.getSmsCode(phone);
    }
    //商户注册
    @PostMapping("/merchants/register")
    public MerchantRegisterVo registerMerchant(@RequestBody MerchantRegisterVo merchantRegisterVo){
        //校验验证码
        smsService.checkVerifiyCode(merchantRegisterVo.getVerifiykey(),merchantRegisterVo.getVerifiyCode());
        //新增商户信息
        //将merchantRegisterVo转换为merchantDto
//        com.huiminpay.merchant.dto.MerchantDTO merchantDTO = new com.huiminpay.merchant.dto.MerchantDTO();
//        merchantDTO.setUsername(merchantRegisterVo.getUsername());
//        merchantDTO.setMobile(merchantRegisterVo.getMobile());
        MerchantDTO merchantDTO = MerchantRegisterConvert.INSTANCE.vo2dto(merchantRegisterVo);
        merchantService.createMerchant(merchantDTO);
        return merchantRegisterVo;
    }
    //文件上传
    @Value("${oss.aliyun.domain}")
    private String domain;
    //桶名称
    @Value("${oss.aliyun.bucket}")
    private String bucket;
    @Autowired
    private OSS oss;
    @ApiOperation("文件上传")
    @ApiImplicitParam(name ="file", value = "上传文件信息")
    @PostMapping("/upload")
    //MultipartFile file里的file要和前端表单里的name一致
    public String upload(@RequestParam("file") MultipartFile file){
        //获取上传的文件名称
        String originalFilename = file.getOriginalFilename();
        //获取文件名的后缀
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //使用UUID生成一个文件名称/也可以用时间戳来生成
        //String fileName = System.currentTimeMillis() + "";
        UUID uuid = UUID.randomUUID();
        //将文件上传到阿里云对象存储中
        try {
            oss.putObject(bucket,uuid+extName,file.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return domain+uuid+extName;
    }
    //获取登录用户的商户信息
    @ApiOperation("获取登录用户的商户信息")
    @GetMapping(value="/my/merchants")
    public MerchantDTO getMyMerchantInfo(){
        // 从token 中获取商户id
        Long merchantId = SecurityUtil.getMerchantId();
        MerchantDTO merchant = merchantService.queryMerchantById(merchantId);
        return merchant;
    }
    //资质申请
    @ApiOperation("资质申请")
    @ApiImplicitParam(name = "merchantInfo", value = "商户认证资料", required = true, dataType = "MerchantDetailVO", paramType = "body")
    @PostMapping("/my/merchants/save")
    public void saveMerchant(@RequestBody MerchantDetailVO merchantInfo){
        //获取当前登录用户的商户id
        Long merchantId = SecurityUtil.getMerchantId();
        //将vo转换为dto
        MerchantDTO merchantDTO = MerchantDetailVOconvert.INSTANCE.vo2dto(merchantInfo);
        merchantService.applyMerchant(merchantId,merchantDTO);
    }
}
